package cn.yqx.bankshop.order.service.impl;

import cn.yqx.bankshop.order.dao.BuGoodsAddressMapper;
import cn.yqx.bankshop.order.dao.BuOrderShipperMapper;
import cn.yqx.bankshop.order.dao.BuOrderTransactRecordMapper;
import cn.yqx.bankshop.order.pojo.*;
import cn.yqx.bankshop.order.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogisticsServiceImpl implements LogisticsService {

    private static final String OPERATE_ACTION = "确认发货" ;
    private static Map<String,String> SHIPPER_MAP = new HashMap<String, String>(30) ;
    @Autowired
    BuOrderTransactRecordMapper mapper ;
    @Autowired
    BuOrderShipperMapper shipperMapper ;
    @Autowired
    BuGoodsAddressMapper addressMapper ;

    @Override
    public List<BuOrderTransactRecord> findLogisticsByNo(Long orderNo) {
        BuOrderTransactRecordExample example = new BuOrderTransactRecordExample() ;
        example.or().andOrderIdEqualTo(orderNo).andOperateActionEqualTo(OPERATE_ACTION) ;
        return mapper.selectByExample(example);
    }

    @Override
    public BuOrderShipper getOrderShipper(String shipperName) {
        BuOrderShipper shipper = new BuOrderShipper() ;
        if(SHIPPER_MAP.containsKey(shipperName)){
            shipper.setShipperCode(SHIPPER_MAP.get(shipperName));
            shipper.setShipperName(shipperName);
        }else{
            BuOrderShipperExample example = new BuOrderShipperExample() ;
            example.or().andShipperNameEqualTo(shipperName) ;
            List<BuOrderShipper> shippers = shipperMapper.selectByExample(example) ;
            if(!shippers.isEmpty()){
                shipper = shippers.get(0) ;
            }
            SHIPPER_MAP.put(shipperName,shipper.getShipperCode()) ;
        }


        return shipper;
    }

    @Override
    public BuGoodsAddress getGoodsAddress(Long addressId) {
        return addressMapper.selectByPrimaryKey(addressId);
    }
}
